import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    count: 100,
  },
  getters: {},
  mutations: {
    addCount(state, value) {
      // 负责增加库存的管家
      state.count += value
    },
    subCount(state, value) {
      // 负责减少库存的管家
      state.count -= value
    },
    setCount(state, value) {
      // 负责直接修改库存的管家
      state.count = value
    },
  },
  actions: {
    asyncAddCount(store, num) {
      setTimeout(() => {
        // 1秒后, 异步提交给add的mutations
        store.commit('addCount', num)
      }, 1000)
    },
    asyncSubCount(store, num) {
      setTimeout(() => {
        // 1秒后, 异步提交给sub的mutations
        store.commit('subCount', num)
      }, 1000)
    },
  },
  modules: {},
})
